//
//  GWBaseNavigationController+GWGetCurrentNAV.m
//  ShandsHotel
//
//  Created by zh_s on 2019/6/24.
//  Copyright © 2019 zh_s. All rights reserved.
//

#import "GWBaseNavigationController+GWGetCurrentNAV.h"

@implementation GWBaseNavigationController (GWGetCurrentNAV)

+ (GWBaseNavigationController *)currentNC
{
    if (![[UIApplication sharedApplication].windows.lastObject isKindOfClass:[UIWindow class]]) {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
    GWBaseViewController *rootViewController = (GWBaseViewController *) [UIApplication sharedApplication].keyWindow.rootViewController;
    return [self getCurrentNCFrom:rootViewController];
}

//递归
+ (GWBaseNavigationController *)getCurrentNCFrom:(GWBaseViewController *)vc
{
    if ([vc isKindOfClass:[UITabBarController class]]) {
        GWBaseNavigationController *nc = ((GWBaseTableViewController *)vc).selectedViewController;
        return [self getCurrentNCFrom:nc];
    }
    else if ([vc isKindOfClass:[UINavigationController class]]) {
        if (((UINavigationController *)vc).presentedViewController) {
            return [self getCurrentNCFrom:((GWBaseNavigationController *)vc).presentedViewController];
        }
        return [self getCurrentNCFrom:((GWBaseNavigationController *)vc).topViewController];
    }
    else if ([vc isKindOfClass:[GWBaseViewController class]]) {
        if (vc.presentedViewController) {
            return [self getCurrentNCFrom:vc.presentedViewController];
        }
        else {
            return vc.navigationController;
        }
    }
    else {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
}

@end
